package com.sanctity.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sanctity.clients.InPatientClient;
import com.sanctity.clients.UserClient;
import com.sanctity.domain.Order;
import com.sanctity.dto.User;
import com.sanctity.feign.clients.PatientClient;
import com.sanctity.feign.dto.patient.PatientDto;
import com.sanctity.feign.vo.patient.PatientVo;
import com.sanctity.mapper.OrderMapper;
import com.sanctity.model.vo.user.UserVo;
import com.sanctity.service.OrderService;
import com.sanctity.utils.MessageProvider;
import com.sanctity.vo.OrderUserVo;
import com.sanctity.vo.OrderVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/***
 * @belongsProject spring-cloud
 * @description <用户服务实现>
 * @author Sanct
 * @version 1.0.0
 * @create 2024/1/16 21:57
 **/
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private MessageProvider messageProvider;

    @Autowired
    private RabbitTemplate docRabbitTemplate;

    @Autowired
    private InPatientClient inPatientClient;

    @Autowired
    private UserClient userClient;

    @Autowired
    private PatientClient patientClient;
    @Override
    public OrderUserVo findWithUserById(Integer id) {
        OrderUserVo orderUserVo = null;
        Order order = orderMapper.selectByPrimaryKey(id);
        if (order.getUserId() != null) {
            /** 硬编码方式服务提供者地址
             String url = "http://127.0.0.1:8009/user/"+id;
             **/
            /**
            // 使用Eureka服务发现通过服务名称调用提供者地址
            String url = "http://inpatientservice/user/" + order.getUserId();
            ResponseEntity<User> forEntity = restTemplate.getForEntity(url, User.class);
            User user = forEntity.getBody();
             **/
            // 使用Feign申明式远程调用微服务
            UserVo user = userClient.findUserById(order.getUserId());

            orderUserVo = new OrderUserVo(order.getId(), order.getUserId(), order.getName(), order.getPrice(), order.getNum());
            orderUserVo.setUser(new User(user.getId(),user.getUsername(),user.getAddress()));
        }
        // 查询患者信息
        if (order.getPatientId() != null){
            // 获取患者信息
            PatientVo patById = patientClient.getPatById(order.getPatientId());
            orderUserVo.setPatientVo(patById);
        }
        return orderUserVo;
    }

    @Override
    public OrderVo addOrder(Order order){
        int insert = orderMapper.insertSelective(order);
        OrderVo orderVo = new OrderVo(order.getId());
        if (insert > 0){
            // 发送订单插入成功到消息队列
            String message = JSONObject.toJSONString(orderVo);
//            rabbitTemplate.convertAndSend("orderAuditExchange","orderAudit.insert",message);
//            docRabbitTemplate.convertAndSend();
            try {
                messageProvider.sendMessage(message);
            } catch (MessageProvider.MQSendException e) {
                // 处理消息发送失败异常
                // 将消息存入缓存中，做消息补发
            }
        }
        return orderVo;
    }


}
